//
//  LJCAFN.m
//  WZLProduct
//
//  Created by 陆静聪 on 16/3/18.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//


#define KEYHEADER @"Authorization"


#import "LJCAFN.h"

@implementation LJCAFN

+ (void)GETWithURL:(NSString *)urlStr
               par:(NSDictionary *)dic
           success:(void (^)(id))responseObj filed:(void (^)(NSError *))err
{
    
    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
    // 有的返回的数据格式，AFN不支持解析，所以我们要设置一下。让AFN支持。
    [man.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [man GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        responseObj(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        err(error);
        
    }];
}

+ (void)POSTWithURL:(NSString *)urlStr prefix:(NSString *)prefix
                par:(NSDictionary *)dic
            success:(void (^)(id))response filed:(void (^)(NSError *))err
{
    
    
    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
    
    NSString *valueHeader = [GTMBase64 encodeBase64String:[NSString stringWithFormat:@"%@:%@", prefix, [LJCAFN getCurrentTime]]];
    
    NSLog(@"%@", valueHeader);
    
    [man.requestSerializer setValue:[NSString stringWithFormat:@"%@", valueHeader] forHTTPHeaderField:KEYHEADER];
    
    [man.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [man POST:urlStr parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        response(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        err(error);
        
    }];
    
    
}

/** 获取时间 */
+ (NSString *)getCurrentTime {

    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"yyyyMMddHHmmss"];
    
    NSString *  locationString=[dateformatter stringFromDate:[NSDate date]];
    return locationString;
    
}


@end
